import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
 
public class Test extends JFrame
{ 
 Graphics g;
 long newTime = System.currentTimeMillis();
 int topHeight = 30;
 int sideWidth = 8;
 double x = 15+sideWidth;
 double y = 15+topHeight;
 double xSpeed = 100.0;
 double ySpeed = 50.0;
  public Test() 
  { 
      setBounds(0,0,500,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      g=getGraphics();
  } 
  public void paint(Graphics g) 
  {
	  long oldTime = newTime;
	  newTime = System.currentTimeMillis();
	  long delTime = Math.abs(oldTime-newTime);
	  if(x>this.getWidth()){
		  x=sideWidth;
	  }
	  if(y>this.getHeight()){
		  y=topHeight;
	  }
	  x+=xSpeed*delTime/1000.0;
	  y+=ySpeed*delTime/1000.0;
	  BufferedImage bimage = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
	  Graphics2D gimage = bimage.createGraphics();
	  Graphics2D g2 = (Graphics2D)g;
	  gimage.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
	  gimage.fillRect(0,0,this.getWidth(), this.getHeight());
	  gimage.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
	  gimage.drawRect(100, 100,300, 300);
	  gimage.drawRect((int)x,(int)y,20,20);
	  if(delTime!=0){
		  gimage.drawString(String.valueOf(1000/delTime), 250,250); 
	  }
   
   //Draws the image to screen
   g2.drawImage(bimage,0,0,null);
  } 
  
  public static void main(String[] a)
  {
   Test t=new Test();
   while(true)
   {
   t.update(t.g);
   try
   {
	   //Keeps framerate at about 60 fps
	   Thread.sleep((long)(16.66666666-(System.currentTimeMillis() - t.newTime)));
   }
   catch(Exception e)
   {
   }
   }
  }
}